﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Models;
using WebSite.App_Code;

namespace WebSite.Controllers
{
    public class PayController : BaseController
    {
        #region Action.Pay

        public ActionResult Pay(string ID)
        {
            PayModel M = new PayModel();
            M.U = this.CurrentUser;

            M.Init(ID);

            return View(M);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Pay(PayModel M, FormCollection collection)
        {
            M.U = this.CurrentUser;

            try
            {
                switch (M.PostingEvent)
                {
                    case L_PageEvent.P_PaySubmit_AddItem:
                        Pay_AddItem(ref M, collection);
                        break;
                    case L_PageEvent.P_PaySubmit_DeleteItem:
                        Pay_DeleteItem(ref M, collection);
                        break;
                    case L_PageEvent.P_PaySubmit_Save:
                        Pay_Save(ref M, collection);
                        break;
                    case L_PageEvent.P_PaySubmit_Submit:
                        Pay_Submit(ref M, collection);
                        break;
                    case L_PageEvent.P_PaySubmit_Print:
                        break;
                    case L_PageEvent.P_PaySubmit_SelAccountCodeDetail:
                        Pay_SelAccountCodeDetail(ref M, collection);
                        break;
                }
            }
            catch (Exception ex)
            {
                ViewData["DB.Operation.Result"] = ex.Message.Replace("'","\"");
            }

            M.Init(M.DS_Pay.Guid);

            return View(M);
        }

        public void Pay_AddItem(ref PayModel M, FormCollection collection)
        {
            M.DS_PayDetail.Add(new PayDetailItem() { });
        }

        public void Pay_DeleteItem(ref PayModel M, FormCollection collection)
        {
            M.DS_PayDetail.RemoveAt(Convert.ToInt32(M.PostingID));
            ModelState.Clear();
        }

        public void Pay_SelAccountCodeDetail(ref PayModel M, FormCollection collection)
        {
            
        }

        public void Pay_Save(ref PayModel M, FormCollection collection)
        {
            ViewData["DB.Operation.Result"] = M.Save();
            ModelState.Clear();
        }

        public void Pay_Submit(ref PayModel M, FormCollection collection)
        {
            ViewData["DB.Operation.Result"] = M.Submit();
            ModelState.Clear();
        }

        #endregion

        #region Action.Pay

        public ActionResult PayQuery(L_Page ID)
        {
            PayQueryModel M = new PayQueryModel();
            M.U = this.CurrentUser;

            M.Init(ID);

            M.Query();

            return View(M);
        }

        #endregion
    }
}
